package com.zmj.service;

import com.zmj.api.impl.CommunionServiceImpl;
import com.zmj.common.api.service.CommunionService;
import com.zmj.registry.RPCServer;
import com.zmj.registry.ZkServiceRegistryCenterImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 用途：
 * @author ：zhangmj
 * @date 2018/9/22 21:40
 */
@Slf4j
@Service("rpcService")
public class RpcService {

    @Autowired
    ZkServiceRegistryCenterImpl zkServiceRegistryCenterImpl;

    @Autowired
    private Environment environment;

    /**
     * RPC端口启动方法
     * @throws IOException
     */
    public void start() {
        try{
            log.info("开始启动RPC服务端端口.......");
            // 客户端需要进行调用某个方法 很多的实现类
            CommunionService communionService = new CommunionServiceImpl();
            String serviceAddress = environment.getProperty("rpc.host");
            /**
             * 服务注册，监听端口 并且和客户端打交道
             * 希望把这个服务注册到zk上
             */
            zkServiceRegistryCenterImpl.registry(CommunionService.class.getName(), serviceAddress);
            RPCServer rpcServer = new RPCServer(zkServiceRegistryCenterImpl,serviceAddress);
            rpcServer.bind(communionService);
            rpcServer.registerAndListen();
            log.info("完成启动RPC服务端端口.......");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
